@echo off
chcp 65001
setlocal enabledelayedexpansion

REM 物业保洁系统快速测试脚本
REM 自动执行基本功能测试

title 物业保洁系统测试工具

set "BASE_URL=http://localhost:8080"
set "FRONTEND_URL=http://localhost:3000"
set "TIMEOUT=30"

REM 初始化计数器
set "TESTS_PASSED=0"
set "TESTS_FAILED=0"
set "TOTAL_TESTS=0"

REM 颜色定义
set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "BLUE=[94m"
set "NC=[0m"

REM 日志函数
:log
echo %GREEN%[%time%]%NC% %~1
goto :eof

:error
echo %RED%[%time%] ERROR:%NC% %~1
goto :eof

:warning
echo %YELLOW%[%time%] WARNING:%NC% %~1
goto :eof

:info
echo %BLUE%[%time%] INFO:%NC% %~1
goto :eof

REM 测试结果记录
:test_result
set /a TOTAL_TESTS+=1
if "%~1"=="0" (
    echo %GREEN%√ %~2%NC%
    set /a TESTS_PASSED+=1
) else (
    echo %RED%× %~2%NC%
    set /a TESTS_FAILED+=1
)
goto :eof

REM 等待服务启动
:wait_for_service
setlocal
set "url=%~1"
set "service=%~2"
call :info "等待 %service% 服务启动..."

for /l %%i in (1,1,30) do (
    curl -s --connect-timeout 5 "%url%" >nul 2>nul
    if !errorlevel! equ 0 (
        call :log "%service% 服务已启动"
        goto :eof
    )
    echo|set /p="."
    timeout /t 2 /nobreak >nul
)

call :error "%service% 服务启动超时"
exit /b 1

REM 测试数据库连接
:test_database
call :info "测试数据库连接..."

REM 检查MySQL容器
docker ps | findstr "property-mysql" >nul
if !errorlevel! equ 0 (
    call :test_result 0 "MySQL容器运行正常"
) else (
    call :test_result 1 "MySQL容器未运行"
)

REM 检查Redis容器
docker ps | findstr "property-redis" >nul
if !errorlevel! equ 0 (
    call :test_result 0 "Redis容器运行正常"
) else (
    call :test_result 1 "Redis容器未运行"
)

goto :eof

REM 测试后端API
:test_backend_api
call :info "测试后端API..."

REM 健康检查
curl -s "%BASE_URL%/actuator/health" | findstr "UP" >nul
if !errorlevel! equ 0 (
    call :test_result 0 "后端健康检查通过"
) else (
    call :test_result 1 "后端健康检查失败"
)

REM 测试用户API
curl -s -w "%%{http_code}" -o nul "%BASE_URL%/api/users" >temp_response.txt
set /p response=<temp_response.txt
del temp_response.txt

if "%response%"=="200" (
    call :test_result 0 "用户API正常"
) else (
    call :test_result 1 "用户API返回 %response%"
)

REM 测试区域API
curl -s -w "%%{http_code}" -o nul "%BASE_URL%/api/areas" >temp_response.txt
set /p response=<temp_response.txt
del temp_response.txt

if "%response%"=="200" (
    call :test_result 0 "区域API正常"
) else (
    call :test_result 1 "区域API返回 %response%"
)

goto :eof

REM 测试前端页面
:test_frontend
call :info "测试前端页面..."

REM 检查首页
curl -s -w "%%{http_code}" -o nul "%FRONTEND_URL%" >temp_response.txt
set /p response=<temp_response.txt
del temp_response.txt

if "%response%"=="200" (
    call :test_result 0 "前端首页可访问"
) else (
    call :test_result 1 "前端首页返回 %response%"
)

goto :eof

REM 测试数据库数据
:test_database_data
call :info "测试数据库数据..."

REM 检查用户表
for /f "tokens=*" %%a in ('docker exec property-mysql-dev mysql -u root -proot123 -e "SELECT COUNT(*) FROM property_cleaning.users;" -s -N 2^>nul') do (
    set "user_count=%%a"
)

if defined user_count (
    if !user_count! gtr 0 (
        call :test_result 0 "用户表有数据 (!user_count! 条)"
    ) else (
        call :test_result 1 "用户表无数据"
    )
) else (
    call :test_result 1 "用户表查询失败"
)

REM 检查区域表
for /f "tokens=*" %%a in ('docker exec property-mysql-dev mysql -u root -proot123 -e "SELECT COUNT(*) FROM property_cleaning.areas;" -s -N 2^>nul') do (
    set "area_count=%%a"
)

if defined area_count (
    if !area_count! gtr 0 (
        call :test_result 0 "区域表有数据 (!area_count! 条)"
    ) else (
        call :test_result 1 "区域表无数据"
    )
) else (
    call :test_result 1 "区域表查询失败"
)

goto :eof

REM 测试性能
:test_performance
call :info "测试性能..."

REM 测试API响应时间
setlocal enabledelayedexpansion
set "start=!time!"
curl -s "%BASE_URL%/api/users" >nul
set "end=!time!"

REM 计算时间差（简化版）
for /f "tokens=1-4 delims=:," %%a in ("!start!") do (
    set /a start_sec=%%a*3600+%%b*60+%%c
)
for /f "tokens=1-4 delims=:," %%a in ("!end!") do (
    set /a end_sec=%%a*3600+%%b*60+%%c
)
set /a duration=end_sec-start_sec

if !duration! lss 1000 (
    call :test_result 0 "API响应时间: !duration!ms"
) else (
    call :test_result 1 "API响应时间过慢: !duration!ms"
)

goto :eof

REM 测试功能完整性
:test_functionality
call :info "测试功能完整性..."

REM 测试用户注册
set "test_user={\"username\":\"test_user\",\"password\":\"test123\",\"email\":\"test@example.com\",\"phone\":\"1234567890\",\"role\":\"USER\"}"
curl -s -w "%%{http_code}" -o nul -X POST -H "Content-Type: application/json" -d "%test_user%" "%BASE_URL%/api/users/register" >temp_response.txt
set /p response=<temp_response.txt
del temp_response.txt

if "%response%"=="201" (
    call :test_result 0 "用户注册功能正常"
) else (
    call :test_result 1 "用户注册失败 (%response%)"
)

REM 测试区域创建
set "test_area={\"name\":\"测试区域\",\"description\":\"测试描述\",\"address\":\"测试地址\",\"manager\":\"测试管理员\",\"phone\":\"1234567890\"}"
curl -s -w "%%{http_code}" -o nul -X POST -H "Content-Type: application/json" -d "%test_area%" "%BASE_URL%/api/areas" >temp_response.txt
set /p response=<temp_response.txt
del temp_response.txt

if "%response%"=="201" (
    call :test_result 0 "区域创建功能正常"
) else (
    call :test_result 1 "区域创建失败 (%response%)"
)

goto :eof

REM 清理测试数据
:cleanup_test_data
call :info "清理测试数据..."

docker exec property-mysql-dev mysql -u root -proot123 -e "DELETE FROM property_cleaning.users WHERE username='test_user';" 2>nul
docker exec property-mysql-dev mysql -u root -proot123 -e "DELETE FROM property_cleaning.areas WHERE name='测试区域';" 2>nul

call :log "测试数据已清理"
goto :eof

REM 生成测试报告
:generate_report
echo.
echo ====================
echo 测试报告
echo ====================
echo 总测试数: %TOTAL_TESTS%
echo 通过: %TESTS_PASSED%
echo 失败: %TESTS_FAILED%
set /a success_rate=%TESTS_PASSED%*100/%TOTAL_TESTS%
echo 成功率: %success_rate%%%
echo.

if %TESTS_FAILED% equ 0 (
    call :log "所有测试通过！系统运行正常"
) else (
    call :warning "部分测试失败，请检查日志"
)
goto :eof

REM 主函数
:main
echo 物业保洁系统快速测试工具
echo ========================
echo.

REM 检查参数
if "%1"=="--help" (
    echo 用法: %~nx0 [选项]
    echo.
    echo 选项:
    echo   --help     显示帮助信息
    echo   --skip-wait 跳过服务启动等待
    echo.
    pause
    goto :eof
)

REM 等待服务启动（除非跳过）
if "%1" neq "--skip-wait" (
    call :wait_for_service "%BASE_URL%/actuator/health" "后端"
    call :wait_for_service "%FRONTEND_URL%" "前端"
)

REM 执行测试
call :test_database
call :test_backend_api
call :test_frontend
call :test_database_data
call :test_performance
call :test_functionality

REM 清理测试数据
call :cleanup_test_data

REM 生成报告
call :generate_report

REM 提供下一步建议
echo.
echo 下一步建议:
echo 1. 访问前端: %FRONTEND_URL%
echo 2. 查看后端文档: %BASE_URL%/swagger-ui.html
echo 3. 检查监控: %BASE_URL%/actuator
echo.

pause
goto :eof

REM 执行主函数
call :main %*